package com.VolunteerPlatform.Controller.system;

import com.VolunteerPlatform.Entity.AdminInfo;
import com.VolunteerPlatform.Entity.AdminRoleInfo;
import com.VolunteerPlatform.SecurityService.MyUserDetailsService;
import com.VolunteerPlatform.Service.AdminService.AdminIdentityService;
import com.VolunteerPlatform.Service.AdminService.AdminInfoService;
import com.VolunteerPlatform.Service.AdminService.AdminRoleService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.dao.SaltSource;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by DoveXL on 2018/12/4.
 */
@Controller
@RequestMapping(value = "/AdminInfo/**")
public class AdminInfoController {
    @Autowired
    private AdminInfoService adminInfoService;

    @Autowired
    private AdminIdentityService adminIdentityService;

    @Autowired
    private AdminRoleService adminRoleService;

    @Autowired
    private MyUserDetailsService myUserDetailsService;

    @Autowired
    private Md5PasswordEncoder md5PasswordEncoder;


    @Autowired
    private SaltSource saltSource;


    @RequestMapping(value = "CertainAdminInfo", method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String getAllAdminInfo(@RequestBody String jsondata, HttpSession httpSession)
    {
        int rowCount=0;
        SecurityContextImpl securityContextImpl = (SecurityContextImpl) httpSession.getAttribute("SPRING_SECURITY_CONTEXT");
        String User = securityContextImpl.getAuthentication().getName();
        JSONObject jsonObject = JSON.parseObject(jsondata);
        int page = jsonObject.getInteger("page");
        int pagesize = jsonObject.getIntValue("pagesize");
        String user_flag = jsonObject.getString("user_flag");
        List<AdminInfo> adminInfoList = adminInfoService.getAllAdminInfoService(null,null,user_flag,-1,-1);
        List<AdminRoleInfo> adminRoleInfoList = adminRoleService.getUserRoleService(null,null,-1,-1);
        System.out.println(adminInfoList.size());
        Map<String,String> map = new HashMap<String ,String>();
        for (int i=0;i<adminRoleInfoList.size();i++)
        {
            map.put(adminRoleInfoList.get(i).getUserName(),adminRoleInfoList.get(i).getAdminID());
        }
        JSONArray UserInfoArray = new JSONArray();
        //List<UserInfo> userInfoList = userManagementService.getFindCertainUserInfo(null,Integer.parseInt(user_flag),page,pagesize);
        try {
            rowCount = adminInfoList.size();
            if(rowCount%pagesize!=0)
            {
                rowCount = rowCount / pagesize + 1;
            }
            else
            {
                rowCount = rowCount / pagesize;
            }
            List<AdminInfo> userInfoList = adminInfoService.getAllAdminInfoService(null,null,user_flag,(page-1)*pagesize,pagesize);
            System.out.println(userInfoList.size());
            for (int i=0;i<userInfoList.size();i++) {
                    JSONObject UserInfo = new JSONObject();
                    UserInfo.put("user_name", userInfoList.get(i).getUserName());
                    UserInfo.put("user_realname", userInfoList.get(i).getRealName());
                    if (userInfoList.get(i).getUserStatus().equals("NotForbidden")) {
                        UserInfo.put("user_status", "正常");
                    } else if (userInfoList.get(i).getUserStatus().equals("Forbidden")) {
                        UserInfo.put("user_status", "禁用");
                    } else {
                        UserInfo.put("user_status", "异常");
                    }
                    if (map.get(userInfoList.get(i).getUserName()).equals("ROLE_ADMIN"))
                    {
                        UserInfo.put("user_role","普通管理员");
                    }else if (map.get(userInfoList.get(i).getUserName()).equals("ROLE_SUPERADMIN"))
                    {
                        UserInfo.put("user_role","超级管理员");
                    }
                    UserInfoArray.add(UserInfo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("pagecount",rowCount);
        jsonObject1.put("currentpage",page);
        jsonObject1.put("pagelist",UserInfoArray);
        return JSON.toJSONString(jsonObject1);
    }






}
